#! /usr/bin/env python
# -*- coding: utf-8 -*-
#Author:pako
#Email:zealzpc@gmail.com
"""
One example for sina weibo search API through Basic Authorization get data
"""
import urllib2
import sys
import re
import base64
import json
import time
from urlparse import urlparse 

theurl = 'http://api.t.sina.com.cn/statuses/search.json?\
count=%(count)d&\
source=%(source_key)d&\
q=%(query_string)s&\
needcount=%(needcount)s&\
starttime=%(starttime)d&\
endtime=%(endtime)d&\
filter_ori=%(filter_ori)d\
'
pass_day_length=3#how much day's data we will to search ,default is one week
search_para={
'count':1,
'source_key':77221908,
'query_string':'帮帮这个可爱的小女孩',#搜索的关键字
'needcount':'true',
#'starttime':time.time()-3600*24*pass_day_length,
'starttime':time.time()-3600*13.3,
'endtime':time.time(),
'filter_ori':5,
}
theurl=theurl%search_para
#theurl='http://api.t.sina.com.cn/statuses/search.json?source=77221908&q=asdf&needcount=10&starttime=1296028369&endtime=1298620369&filter_ori=5'
print 'theurl:',theurl
#province=
#city=
username = 'zealzpc@qq.com'
password = 'zhangpei'
base64string = base64.encodestring(
                '%s:%s' % (username, password))[:-1]
authheader =  "Basic %s" % base64string
req = urllib2.Request(theurl)
print "authheader:",authheader
req.add_header("Authorization", authheader)
try:
    handle = urllib2.urlopen(req)
except IOError, e:
    # here we shouldn't fail if the username/password is right
    print "It looks like the username or password is wrong."
    sys.exit(1)
thepage = handle.read()
print  "thepage:",thepage
res= json.read(thepage)


print type(res),res
print "====================The result of %d day's data===================="%pass_day_length
total_count = res['total_count_maybe']
result = res['results']
print 'total_count_maybe:',total_count
print 'result len:',len(result)
for row in result:
    text=row['text']
    print row['id']
    print text
    print row
print "====================end===================="